//logs.js
const util = require('../../utils/util.js')
const app = getApp()
Page({
  data: {
    falg: true,
    resourceList: [],
    detailList: [],
    message: '加载更多',
    num: 1
  },
  //flag改变
  flagChange: function(e) {
    this.setData({
      falg: e.detail
    })
    if (this.data.falg) {
      this.setData({
        detailList: []
      })
    }
  },
  detailList: function(e) {
    this.setData({
      detailList: e.detail
    })
  },
  onReachBottom: function() {
    let that = this
    if (that.data.message == '加载更多') {
      that.getMore()
    }
  },
  //加载更多
  getMore: function() {
    const that = this
    wx.cloud.callFunction({
      name: 'article',
      data: {
        num: that.data.num,
        database: 'resources'
      },
      success: res => {
        if (res.result.data.length < 10) {
          that.setData({
            message: '没有数据了'
          })
        }
        that.setData({
          resourceList: that.data.resourceList.concat(res.result.data),
          num: that.data.num++
        })
      },
      fail: res => {
        console.log('失败', res)
      }
    })
  },
  //显示提示信息
  toVs: function(event) {
    if (!wx.getStorageSync('userInfo').openid) {
      wx.showModal({
        title: '请先登录',
        content: '您还未登录，请先登录',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/me/message/message',
            })
          }
        }
      })
    } else {
      //添加历史记录
      let history = wx.getStorageSync('history')
      if (!history) {
        history = []
      }
      let obj = {
        title: event.currentTarget.dataset.title,
        timer: util.formatTime(new Date())
      }
      history.push(obj)
      wx.setStorageSync('history', history)

      const link = event.currentTarget.dataset.link
      wx.showModal({
        title: '百度网盘地址',
        content: '链接:' + link.url + '-提取码:' + link.key,
        showCancel: false,
        confirmText: '复制链接',
        success(res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: '链接:' + link.url + '-提取码:' + link.key,
              success(res) {
                wx.showToast({
                  title: '复制成功',
                  duration: 1000
                })
              }
            })
          }
        }
      })
    }


  },
  //页面加载
  onLoad: function(options) {
    wx.showToast({
      title: '正在加载',
      icon: 'loading',
      duration: 600
    })
    const that = this
    wx.cloud.callFunction({
      name: 'article',
      data: {
        num: 0,
        database: 'resources'
      },
      success: res => {
        that.setData({
          resourceList: res.result.data
        })
      },
      fail: res => {
        console.log(res)
      }
    })
  }
})